package cn.liberals.org.controller;

import cn.liberals.basic.exception.BusinessException;
import cn.liberals.basic.utils.ExcelUtil;
import cn.liberals.org.domain.ShopAuditLog;
import cn.liberals.org.service.IShopService;
import cn.liberals.org.domain.Shop;
import cn.liberals.org.query.ShopQuery;
import cn.liberals.basic.utils.AjaxResult;
import cn.liberals.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.common.ExpressionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryData(query);
    }

    
    /* 
    * @Title: settlement
    * @Description: 店铺入驻接口
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/1 20:17
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /* 
    * @Title: pass
    * @Description: 店铺通过接口
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/2 23:09  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /*
    * @Title: pass
    * @Description: 店铺驳回接口
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/2 23:10
    * @Parameters:
    * @Return
    */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常，请稍后重试！");
        }
    }

    @PostMapping("/audit/js")
    public AjaxResult js(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.js(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /*
    * @Title: exportExcel的导出
    * @Description: 导出
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/2 23:22
    * @Parameters:
    * @Return
    */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse resp){
        // 查询所有店铺
        List<Shop> shops = shopService.loadAll();
        // 使用excel导出
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xlsx",resp);
    }

    /*
    * @Title: importExcel的导入
    * @Description: 导入
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/2 23:45
    * @Parameters:
    * @Return
    */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        // 用工具类来掉用导入
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        // 批量新增
        shopService.batchSave(shops);
        System.out.println(shops);
    }

    /*
    * @Title: loadShopEchartsData
    * @Description: 报表的建造
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/4 1:23
    * @Parameters:
    * @Return
    */
    @GetMapping("/echarts")
    public Map<String, Object> loadShopEchartsData(){
        return shopService.loadShopEchartsData();
    }

    /* 
    * @Title: activation
    * @Description: 激活页面
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/3 23:57  
    * @Parameters: 
    * @Return 
    */
    @GetMapping("/activation/{id}")
    public AjaxResult activation(@PathVariable("id") Long id){
        try {
            shopService.activation(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /* 
    * @Title: settlementAgain
    * @Description: 驳回重新入驻接口
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/3 23:58  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/settlementAgain")
    public AjaxResult settlementAgain(@RequestBody Shop shop){
        try {
            shopService.settlementAgain(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常，请稍后重试！");
        }
    }
}
